
$(function(){
	init();
});

var addFlag = false;

var sessAppBase = sessionStorage.getItem("appBase");
var sendData = JSON.parse(sessAppBase);
var orderId = sendData.orderId;
var appId = sendData.appId;
var sign = sendData.sign;
var preprintedNo="";
var pdfurl="";
var quoBase;
var policyRef = "";
var subBranchCode = "";

	function init(){
		padMarketing.ajaxNormal('bankAuthInit.action',{},
			function(data){
				pdfurl = data.pdfPortal+"/Interface/iPad/NL6012?orderId="+orderId+"&appId="+appId+"&sign="+sign;
				
				var editPayAppBase = sessionStorage.getItem("editPayAppBase");
				editPayAppBase = JSON.parse(editPayAppBase);
				quoBase = editPayAppBase;
				var pmBaBankCategoryId = editPayAppBase.pmBaBankCategoryId;
				var pmBaNo = editPayAppBase.pmBaNo;
				var pmBaAreaCode = editPayAppBase.pmBaAreaCode;
				policyRef = editPayAppBase.policyRef;
				preprintedNo = editPayAppBase.preprintedNo;
				pdfurl += "&pmBaBankCategoryId="+pmBaBankCategoryId+"&pmBaAreaCode="+pmBaAreaCode+"&pmBaNo="+pmBaNo;
				//调壳初始化签字配置
				console.log(editPayAppBase)
				//广东用户判断直接通过
				subBranchCode = editPayAppBase.subBranchCode;
				if(editPayAppBase.subBranchCode == "GD") {
					$("#change-btn").removeClass("btn-warning").addClass("btn-success").html("完成");
					addFlag = true;
				}

				initSign(policyRef,'2');
				
				
				
			},
			function(data){
			}
		);
	}
	
	function gotoPdfShow(){
		goPdfShow(pdfurl,0,"N");
	}

	/*function goSign1(){
		showSign("../html/img/16-2-1.png",'img1');
	}*/
	
	function goHomenow(){
		alertMsg("支付方式还未修改，是否放弃本次操作？",1,function(){
			window.location="/pad_marketing/index/index.action";
		});
	};
	
	function showSign(src,pdfNum,siger,imgNum){
		
		var num = imgNum.substr(3,1);
		$("#sign"+num).hide();
		var img=document.getElementById(imgNum);
		src = 'data:image/png;base64,' + src;
		img.src = src;
		if(myScroll)
			myScroll.refresh();
	}
	
	/**
	 * 展示拍照头像
	 * 
	 * @param src
	 * @param imgNum
	 */

	var src3 = "";
	var src4 = "";
	function showPhoto(src,imgNum){
		if('pho3'==imgNum)src3 = src;
		if('pho4'==imgNum)src4 = src;
		var img=document.getElementById(imgNum);
		src = 'data:image/png;base64,' + src;			
		img.src = src;
		if(myScroll)
			myScroll.refresh();
	}
	
	function signComplete(){
		//校验是否签字完成
		var sign1 = $("#sign1").attr("style");
		var sign2 = $("#sign2").attr("style");
		var otherSign = $("#otherSign").attr("style");
		
		if('display: none;'!= sign1  ){
			alertMsg('请完成投保人签名！');
			return;
		}
		//所需投保人拍照
		if('display: none;'!= otherSign && (''==src3)){
			alertMsg('请完成投保人上半身拍照！ ');
			return;
		}
		
		if('display: none;'!=sign2){
			alertMsg('请完成营销人员签名！');
			return;
		};

		//营销人员拍照
		if('display: none;'!= otherSign && (''==src4)){
			alertMsg('请完成营销人员上半身拍照！ ');
			return;
		}
		
					
		//广东用户判断直接通过
		if(subBranchCode == "GD") {
			//alertMsg("您的首期、续期缴费账户将改为："+quoBase.pmBaNo+"<br>开户行为："+quoBase.pmBaName+"<br>开户地为："+quoBase.pmBaProvince+"省（直辖市）"+quoBase.pmBaCityName,1,function(){
				//调壳生成加密字符串
				finishSign("doSign");
    		//});
		}else{
			//alertMsg("您的首期、续期缴费账户将改为："+quoBase.pmBaNo+"<br>开户行为："+quoBase.pmBaName+"<br>开户地为："+quoBase.pmBaProvince+"省（直辖市）"+quoBase.pmBaCityName,1,function(){
				finishSign("doSign");
			//});
		}
		
		
	}
	
	var src1 = "";
	var src2 = "";
	function doSign(pdf3){
		var photoList1 = [src1,src2,src3,src4];
		var pdfMessageList = [{"pdfType":"1164","pdfURL":pdfurl,"encode":pdf3,"photoList":photoList1}];
		var params={"appId":appId,"orderId":orderId,"sign":sign,"insured":preprintedNo,"pdfMessageList":pdfMessageList,"policyRef":policyRef};
		
		if(subBranchCode == "GD"){

		 var editPayAppBase = {};
		 if (sessionStorage.editPayAppBase && sessionStorage.editPayAppBase.length > 0) {		
			 editPayAppBase = JSON.parse(sessionStorage.editPayAppBase);
		 }
		 preprintedNo=editPayAppBase.preprintedNo;		
		 policyRef=editPayAppBase.policyRef;
		 nbPremium = editPayAppBase.nbPremium;
		 productName=editPayAppBase.productName;
		 sign=editPayAppBase.sign;
		 appId=editPayAppBase.appId;
		 orderId=editPayAppBase.orderId;
			
    	  padMarketing.ajaxNormal("/pad_marketing/main/appSaveChgNBPay.action",editPayAppBase,
					function (data){
		    		  if(editPayAppBase.subBranchCode=="GD" && editPayAppBase.nbPaymentMethodName=="银行自动转账"){
							if(editPayAppBase.paymentModeName=="一次交清"){
								alertMsg("恭喜您投保成功，请确保您的银行卡余额充足。同时可到订单管理页面查询投保单处理进度",1,function(){
									window.location.href='/pad_marketing/main/paySuccess.action?sucFlag=1&preprintedNo='+preprintedNo+'&policyRef='+policyRef+'&sign='+sign+'&appId='+appId+'&orderId='+orderId;
									
								})
							}
							else{												
								alertMsg("恭喜您投保成功，请确保您的银行卡余额充足。同时可到订单管理页面查询投保单处理进度",1,function(){

									window.location.href='/pad_marketing/main/paySuccess.action?sucFlag=1&preprintedNo='+preprintedNo+'&policyRef='+policyRef+'&sign='+sign+'&appId='+appId+'&orderId='+orderId;
								
								})
							}
						}
						else
						{
							alertMsg('恭喜您投保成功，请确保您的银行卡余额充足。同时可到订单管理页面查询投保单处理进度。',0,function(){
								window.location.href='/pad_marketing/main/paySuccess.action?sucFlag=1&preprintedNo='+preprintedNo+'&policyRef='+policyRef+'&sign='+sign+'&appId='+appId+'&orderId='+orderId;
							})
						}		
						
					},
					function(data){
						console.log(data);
					},
					true,//是否异步
					true,//是否显示加载电波图
					'不用美图都那么帅，不过字该练练啦！'//电波图中的话术
    	  );
		}
		else{
			padMarketing.ajaxNormal('bjcapdf.action',params,
					function(data){
						var flag = data.flag;
						if('Y'==flag){
							if(addFlag) {
								window.location.href='/pad_marketing/main/paySuccess.action?sucFlag=1&preprintedNo='+quoBase.preprintedNo+'&policyRef='+quoBase.policyRef+'&sign='+quoBase.sign+'&appId='+quoBase.appId+'&orderId='+quoBase.orderId;
							} else {
								window.location="cardPhoto.action?signtoupload=2";
							}
						}
					},function(data){
						
					},
			     	true,//是否异步
					true,//是否显示加载电波图
					'不用美图都那么帅，不过字该练练啦！'//电波图中的话术
			);
		}
		
		
		
		
		
	}
